package com.carrental.vehicle.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carrental.common.result.Result;
import com.carrental.vehicle.dto.ImageCollectionDTO;
import com.carrental.vehicle.entity.Image;
import com.carrental.vehicle.entity.ImageCollection;
import com.carrental.vehicle.service.ImageCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图片集合控制器
 */
@Slf4j
@Api(tags = "图片集合管理")
@RestController
@RequestMapping("/image/collection")
@RequiredArgsConstructor
public class ImageCollectionController {

    private final ImageCollectionService imageCollectionService;

    /**
     * 分页查询图片集合
     */
    @ApiOperation("分页查询图片集合")
    @GetMapping("/page")
    public Result<IPage<ImageCollection>> pageList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("图片集名称") @RequestParam(required = false) String name,
            @ApiParam("分类ID") @RequestParam(required = false) Long categoryId) {
        IPage<ImageCollection> page = imageCollectionService.pageList(current, size, name, categoryId);
        return Result.success(page);
    }

    /**
     * 获取图片集合详情
     */
    @ApiOperation("获取图片集合详情")
    @GetMapping("/{id}")
    public Result<ImageCollection> getById(@ApiParam("图片集合ID") @PathVariable Long id) {
        ImageCollection collection = imageCollectionService.getById(id);
        return Result.success(collection);
    }

    /**
     * 新增图片集合
     */
    @ApiOperation("新增图片集合")
    @PostMapping
    public Result<Boolean> add(@ApiParam("图片集合信息") @RequestBody ImageCollection imageCollection) {
        boolean result = imageCollectionService.addCollection(imageCollection);
        return Result.success(result);
    }

    /**
     * 修改图片集合
     */
    @ApiOperation("修改图片集合")
    @PutMapping
    public Result<Boolean> update(@ApiParam("图片集合信息") @RequestBody ImageCollection imageCollection) {
        boolean result = imageCollectionService.updateCollection(imageCollection);
        return Result.success(result);
    }

    /**
     * 删除图片集合
     */
    @ApiOperation("删除图片集合")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@ApiParam("图片集合ID") @PathVariable Long id) {
        boolean result = imageCollectionService.deleteCollection(id);
        return Result.success(result);
    }

    /**
     * 修改图片集合状态
     */
    @ApiOperation("修改图片集合状态")
    @PutMapping("/status")
    public Result<Boolean> updateStatus(
            @ApiParam("图片集合ID") @RequestParam Long id,
            @ApiParam("状态") @RequestParam Integer status) {
        boolean result = imageCollectionService.updateStatus(id, status);
        return Result.success(result);
    }

    /**
     * 获取图片集合中的图片列表
     */
    @ApiOperation("获取图片集合中的图片列表")
    @GetMapping("/images")
    public Result<IPage<Image>> getCollectionImages(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "24") Integer size,
            @ApiParam("图片集合ID") @RequestParam Long collectionId) {
        IPage<Image> page = imageCollectionService.getCollectionImages(current, size, collectionId);
        return Result.success(page);
    }

    /**
     * 向图片集合中添加图片
     */
    @ApiOperation("向图片集合中添加图片")
    @PostMapping("/add-image")
    public Result<Boolean> addImageToCollection(@RequestBody ImageCollectionDTO imageCollection) {

        boolean result = imageCollectionService.addImageToCollection(imageCollection, null);
        return Result.success(result);
    }

    /**
     * 批量向图片集合中添加图片
     */
    @ApiOperation("批量向图片集合中添加图片")
    @PostMapping("/add-images")
    public Result<Boolean> batchAddImagesToCollection(@RequestBody BatchImageRelationRequest request) {
        boolean result = imageCollectionService.batchAddImagesToCollection(request.getCollectionId(), request.getImageIds());
        return Result.success(result);
    }

    /**
     * 从图片集合中移除图片
     */
    @ApiOperation("从图片集合中移除图片")
    @PostMapping("/remove-image")
    public Result<Boolean> removeImageFromCollection(@RequestBody ImageRelationRequest request) {
        boolean result = imageCollectionService.removeImageFromCollection(request.getCollectionId(), request.getImageId());
        return Result.success(result);
    }

    /**
     * 批量从图片集合中移除图片
     */
    @ApiOperation("批量从图片集合中移除图片")
    @PostMapping("/batch-remove")
    public Result<Boolean> batchRemoveImagesFromCollection(@RequestBody BatchImageRelationRequest request) {
        boolean result = imageCollectionService.batchRemoveImagesFromCollection(request.getCollectionId(), request.getImageIds());
        return Result.success(result);
    }
    
    /**
     * 图片关联请求
     */
    @Data
    public static class ImageRelationRequest {
        /**
         * 图片集合ID
         */
        private Long collectionId;
        
        /**
         * 图片ID
         */
        private Long imageId;
    }
    
    /**
     * 批量图片关联请求
     */
    @Data
    public static class BatchImageRelationRequest {
        /**
         * 图片集合ID
         */
        private Long collectionId;
        
        /**
         * 图片ID列表
         */
        private List<Long> imageIds;
    }

    /**
     * 测试链路追踪
     */
    @ApiOperation("测试链路追踪")
    @GetMapping("/trace-test")
    public Result<String> traceTest() {
        log.info("这是一个测试链路追踪的接口");
        return Result.success("测试链路追踪成功");
    }
} 